package com.kgc.ssm.controller;


import com.kgc.ssm.bean.Provider;
import com.kgc.ssm.constant.CommConstant;
import com.kgc.ssm.service.ProviderService;
import com.kgc.ssm.util.PageSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.util.List;
@Controller
public class ProviderController{

    @Autowired
    private ProviderService providerService;


    /**
     * @author : shiguangchuan
     * @date   : 2022/8/15
     * @param  : [request, response]
     * @return : void
     * @description : 查询供应商列表，带分页
     */
    public void providerList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取查询参数
        String queryProCode = request.getParameter("queryProCode");
        String queryProName = request.getParameter("queryProName");

        // 根据查询条件，查询总条数
        int totalCount = providerService.getProviderCountByCondition(queryProCode, queryProName);

        // 获取分页参数
        Integer pageNo = request.getParameter("pageNo") == null ? CommConstant.DEFAULT_INIT_PAGE_NO : Integer.valueOf(request.getParameter("pageNo"));
        Integer pageSize = request.getParameter("pageSize") == null ? CommConstant.DEFAULT_INIT_PAGE_SIZE : Integer.valueOf(request.getParameter("pageSize"));

        // 创建分页对象，封装参数
        PageSupport<List<Provider>> pageSupport = new PageSupport<>();
        pageSupport.setPageSize(pageSize);
        pageSupport.setTotalCount(totalCount);

        // 页码特殊处理
        if(pageNo < 1 || pageSupport.getTotalPage() == 0){
            pageNo = 1;
        } else if(pageNo > pageSupport.getTotalPage()){
            pageNo = pageSupport.getTotalPage();
        }

        // 设置当前页码
        pageSupport.setCurrPageNo(pageNo);

        // 查询所有的供应商详情列表，带分页
        List<Provider> providerListPage = providerService.getProviderListByCondition(queryProCode, queryProName, pageSupport.getCurrPageNo(), pageSupport.getPageSize());

        // 添加分页数据
        pageSupport.setData(providerListPage);

        // 将用户列表放到request作用域中
        request.setAttribute("pageSupport", pageSupport);

        // 查询条件回显，将原查询条件，放入作用域，页面重新获取
        request.setAttribute("queryProCode", queryProCode);
        request.setAttribute("queryProName", queryProName);

        // 内部转发到用户列表页面
        request.getRequestDispatcher(request.getContextPath() + "/jsp/providerlist.jsp").forward(request, response);
    }

    /**
     * @author : zhukang
     * @date   : 2022/8/12
     * @param  : [request, response]
     * @return : void
     * @description : 删除供应商详情
     */
    public void providerDel(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取删除供应商详情的编号
        String providerId = request.getParameter("id");

        // 调用业务接口，根据供应商编号，删除供应商详情
        providerService.delProviderById(Integer.valueOf(providerId));

        // 重定向到供应商列表页面
        response.sendRedirect("/providers?method=providerList");
    }

    /**
     * @author : zhukang
     * @date   : 2022/8/12
     * @param  : [request, response]
     * @return : void
     * @description : 查看供应商详情
     */
    public void toViewProvider(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取查看的供应商详情的编号
        String providerId = request.getParameter("id");

        // 调用业务接口，根据供应商编号，查询供应商详情
        Provider provider = providerService.getProviderById(Integer.valueOf(providerId));

        // 将供应商详情对象，存入作用域中
        request.setAttribute("provider", provider);

        // 转发到到供应商详情页面
        request.getRequestDispatcher("/jsp/providerview.jsp").forward(request, response);
    }

    /**
     * @author : zhukang
     * @date   : 2022/8/12
     * @param  : [request, response]
     * @return : void
     * @description : 跳转修改供应商详情页面
     */
    public void toModProvider(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取修改的供应商详情的编号
        String providerId = request.getParameter("id");

        // 调用业务接口，根据供应商编号，查询供应商详情
        Provider provider = providerService.getProviderById(Integer.valueOf(providerId));

        // 将供应商详情对象，存入作用域中
        request.setAttribute("provider", provider);

        // 转发到到供应商详情页面
        request.getRequestDispatcher("/jsp/providermodify.jsp").forward(request, response);
    }

    /**
     * @author : zhukang
     * @date   : 2022/8/12
     * @param  : [request, response]
     * @return : void
     * @description : 修改供应商详情
     */
    public void providerMod(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, ParseException {
        // 获取修改供应商详情的参数
        Provider providerForm = new Provider();
        providerForm.setId(Integer.valueOf(request.getParameter("pid")));
        providerForm.setProName(request.getParameter("proName"));
        providerForm.setProContact(request.getParameter("proContact"));
        providerForm.setProPhone(request.getParameter("proPhone"));
        providerForm.setProAddress(request.getParameter("proAddress"));
        providerForm.setProFax(request.getParameter("proFax"));
        providerForm.setProDesc(request.getParameter("proDesc"));
        providerForm.setModifyBy((Integer) request.getSession().getAttribute("userId"));

        // 调用业务接口，修改供应商详情
        providerService.modProviderById(providerForm);

        // 重定向到供应商列表页面
        response.sendRedirect("/providers?method=providerList");
    }

    /**
     * @author : zhukang
     * @date   : 2022/8/12
     * @param  : [request, response]
     * @return : void
     * @description : 增加供应商详情
     */
    public void providerAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, ParseException {
        // 获取增加供应商详情的参数
        Provider providerForm = new Provider();
        providerForm.setProCode(request.getParameter("proCode"));
        providerForm.setProName(request.getParameter("proName"));
        providerForm.setProContact(request.getParameter("proContact"));
        providerForm.setProPhone(request.getParameter("proPhone"));
        providerForm.setProAddress(request.getParameter("proAddress"));
        providerForm.setProFax(request.getParameter("proFax"));
        providerForm.setProDesc(request.getParameter("proDesc"));
        providerForm.setCreatedBy((Integer) request.getSession().getAttribute("userId"));

        // 调用业务接口，添加供应商详情
        providerService.addProviderInfo(providerForm);

        // 重定向到供应商列表页面
        response.sendRedirect("/providers?method=providerList");
    }
}
